package com.yunhai.device.service.impl;

import com.yunhai.device.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.device.mapper.MachineDeployTemplateMapper;
import com.yunhai.model.entity.dms.DmsMachineDeployTemplateEntity;
import com.yunhai.model.vo.dms.DmsMachineDeployTemplateQuery;
import com.yunhai.model.vo.dms.DmsMachineDeployTemplateVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MachineDeployTemplateServiceImpl implements MachineDeployTemplateService {

    @Resource
    private MachineDeployTemplateMapper machineDeployTemplateMapper;

    /**
     * 获取 MachineDeployTemplate 分页列表
     */
    @Transactional
    public IPage getMachineDeployTemplatePagination(PaginationVo paginationVo, DmsMachineDeployTemplateQuery dmsMachineDeployTemplateQuery ) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<DmsMachineDeployTemplateEntity> lqs = new LambdaQueryWrapper();
        machineDeployTemplateMapper.selectPage(page, lqs);

        List<DmsMachineDeployTemplateEntity> record = page.getRecords();
        if( !ObjectUtil.isNotEmptyList(record)){
            return page;
        }
        return page;
    }


    /**
     * 获取 MachineDeployTemplate 所有数据
     *
     */
    @Transactional
    public List<DmsMachineDeployTemplateEntity> getMachineDeployTemplateAllList( DmsMachineDeployTemplateQuery dmsMachineDeployTemplateQuery ) throws Exception {

        LambdaQueryWrapper<DmsMachineDeployTemplateEntity> lqs = new LambdaQueryWrapper();
        List<DmsMachineDeployTemplateEntity> list = machineDeployTemplateMapper.selectList(lqs);
        return list;
    }


    /**
     * 获取 MachineDeployTemplate 详情
     */
    @Transactional
    public DmsMachineDeployTemplateEntity getMachineDeployTemplateDetail( Long id) throws Exception {

        DmsMachineDeployTemplateEntity dmsMachineDeployTemplateEntity = machineDeployTemplateMapper.selectById(id);
        return dmsMachineDeployTemplateEntity;
    }


    /**
     * 新增 MachineDeployTemplate
     * */
    @Transactional
    public void addMachineDeployTemplate(DmsMachineDeployTemplateVo dmsMachineDeployTemplateVo) throws Exception {

        //1、设置 2 个月的过期时间。
        //2、设置 storeId
        DmsMachineDeployTemplateEntity dmsMachineDeployTemplateEntity = new DmsMachineDeployTemplateEntity();
        ReflectionCopy.copyProperties( dmsMachineDeployTemplateEntity, dmsMachineDeployTemplateVo );
        machineDeployTemplateMapper.insert(dmsMachineDeployTemplateEntity);
        return;
    }


    /**
     * 更新 MachineDeployTemplate
     * */
    @Transactional
    public void updateMachineDeployTemplate( DmsMachineDeployTemplateVo dmsMachineDeployTemplateVo) throws Exception {

        if( dmsMachineDeployTemplateVo.getId() == null ){
            throw GlobalException.gen(10021, "id不存在");
        }
        DmsMachineDeployTemplateEntity dmsMachineDeployTemplateEntity = new DmsMachineDeployTemplateEntity();
        ReflectionCopy.copyProperties( dmsMachineDeployTemplateEntity, dmsMachineDeployTemplateVo );
        machineDeployTemplateMapper.updateById(dmsMachineDeployTemplateEntity);
        return;
    }

    /**
     * 删除 MachineDeployTemplate
     * */
    @Transactional
    public void batchRemoveMachineDeployTemplate( List<Long> ids ) throws Exception {

        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        machineDeployTemplateMapper.deleteBatchIds(ids);
    }

}